package gen

import (
	"path/filepath"
	"server/application/constant/keys"
	"server/pkg/configs"
	"strings"
)

// NameTypeTs 获取最终写入的实体名称（可以是别名）
const (
	NameCameBig   = iota //大驼峰命名法
	NameCameSmall        //小驼峰命名法
	NameLine             //下划线命名法
)

func ServerDir() string {
	dir := configs.GetConf().GetString(keys.ConfigKeyGenServeDir)
	if dir == "" {
		return GetDefaultRootDir()
	}
	return dir
	//return setting.ServeDir
}

func FrontDir() string {
	dir := configs.GetConf().GetString(keys.ConfigKeyGenFrontDir)
	if dir == "" {
		return strings.ReplaceAll(GetDefaultRootDir(), "server", "front")
	}
	return dir
	//return setting.FrontDir
}
func TemplateDir() string {
	return filepath.Join(ServerDir(), "application", "auto", "templates")
}

// GetGoDirEntityConfig 获取实体所在文件夹
func GetGoDirEntityConfig() string {
	return filepath.Join(ServerDir(), "application", "auto", "entitys")
}

// GetGoPathRouter 获取路由文件路径
func GetGoPathRouter(entityInfo *EntityInfo) string {
	fileName := NameToLine(entityInfo.EntityName) //使用下划线命名法
	return filepath.Join(ServerDir(), "application", "router", "router_"+fileName+".go")
}

// GetGoPathDto dto 路径
func GetGoPathDto(entityInfo *EntityInfo) string {
	fileName := GetRealFileName(entityInfo, NameLine)
	return filepath.Join(ServerDir(), "application", "db", "schema", "sdto", fileName+".go")
}

// GetTsPathDto dto 路径
func GetTsPathDto(entityInfo *EntityInfo) string {
	fileName := GetRealFileName(entityInfo, NameCameBig)
	return filepath.Join(FrontDir(), "src", "api", "dbdto", "dto"+fileName+".ts")
}

// GetGoPathController 获取控制器路径
func GetGoPathController(entityInfo *EntityInfo) string {
	fileName := GetRealFileName(entityInfo, NameLine)
	return filepath.Join(ServerDir(), "application", "controller", "custctrl", fileName+".go")
}

// GetGoPathService 获取dbservice路径
func GetGoPathService(entityInfo *EntityInfo) string {
	fileName := GetRealFileName(entityInfo, NameLine)
	return filepath.Join(ServerDir(), "application", "db", "schema", "tables", fileName+".go")
}

// GetGoPathGenerics 获取泛型定义输出文件，该文件路径固定（弃用）
func GetGoPathGenerics() string {
	return filepath.Join(ServerDir(), "application", "db", "repository", "generics.go")
}

// GetGoPathApiConst api常量定义文件，该文件路径固定
func GetGoPathApiConst(entityInfo *EntityInfo) string {
	fileName := GetRealFileName(entityInfo, NameLine)
	return filepath.Join(ServerDir(), "application", "constant", "apis", fileName+".go")
}

// GetGoDirConst api常量定义文件，该文件路径固定
func GetGoDirConst() string {
	return filepath.Join(ServerDir(), "application", "constant")
}
func GetGoDirApplication() string {
	return filepath.Join(ServerDir(), "application")
}

// GetGoPathRouterProtect 获取路径文件路径
func GetGoPathRouterProtect() string {
	return filepath.Join(ServerDir(), "application", "router", "protect_apis.go")
}

// GetTsDirConstDefault go常量默认生成路径
func GetTsDirConstDefault() string {
	return filepath.Join(FrontDir(), "src", "constant")
}

// GetTsDirConstApis api常量 前段存储路径
func GetTsDirConstApis() string {
	return filepath.Join(FrontDir(), "src", "api")
}

// GetTsxPathColumns columns.tsx
func GetTsxPathColumns(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//fileName := GetRealFileName(entityInfo, NameBig)
	return filepath.Join(FrontDir(), "src", "views", midDir, "columns", "list.tsx")
}

// GetTsxPathColumnsResetRender columns.tsx
func GetTsxPathColumnsResetRender(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//fileName := GetRealFileName(entityInfo, NameBig)
	return filepath.Join(FrontDir(), "src", "views", midDir, "columns", "resetList.tsx")
}

// GetTsPathColumnsDataRule 编辑表单通用配置（包括更新和新增）
func GetTsPathColumnsDataRule(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//fileName := GetRealFileName(entityInfo, NameBig)
	return filepath.Join(FrontDir(), "src", "views", midDir, "columns", "data.ts")
}

// GetTsPathResetUpdateRule columns.tsx
func GetTsPathResetUpdateRule(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//fileName := GetRealFileName(entityInfo, NameBig)
	return filepath.Join(FrontDir(), "src", "views", midDir, "columns", "resetUpdate.ts")
}

// GetTsPathResetCreateRule 获取创建表单rule
func GetTsPathResetCreateRule(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//fileName := GetRealFileName(entityInfo, NameBig)
	return filepath.Join(FrontDir(), "src", "views", midDir, "columns", "resetCreate.ts")
}

func GetTsPathRequest(entityInfo *EntityInfo) string {
	//midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	fileName := GetRealFileName(entityInfo, NameCameSmall)
	//front/src/api/modules
	return filepath.Join(FrontDir(), "src", "api", "modules", fileName+".ts")
}

// GetFrontUserDirConfig 构建获取前端部分目录
func GetFrontUserDirConfig(entityInfo *EntityInfo) string {
	EntityName := GetRealFileName(entityInfo, NameCameSmall)
	if entityInfo.UserConfig != nil && entityInfo.UserConfig.ViewsFatherFolder != "" {
		return filepath.Join(entityInfo.UserConfig.ViewsFatherFolder, EntityName)
	}
	return EntityName
}

// GetVueIndexPath 获取列表首页index.vue
func GetVueIndexPath(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//front/src/views/developer/sysDictionary/index.vue
	return filepath.Join(FrontDir(), "src", "views", midDir, "index.vue")
}

// GetVueCreatePath 获取创建 create.vue
func GetVueCreatePath(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//front/src/views/developer/sysDictionary/components/create.vue
	return filepath.Join(FrontDir(), "src", "views", midDir, "components", "create.vue")
}

// GetVueUpdatePath 获取创建 create.vue
func GetVueUpdatePath(entityInfo *EntityInfo) string {
	midDir := GetFrontUserDirConfig(entityInfo) //获取中间目录
	//front/src/views/developer/sysDictionary/components/create.vue
	return filepath.Join(FrontDir(), "src", "views", midDir, "components", "update.vue")
}

// GetPathTemplate  获取某个模板路径
func GetPathTemplate(TemplateName string) string {
	return filepath.Join(TemplateDir(), TemplateName+".tmpl")
}

// GetRealFileName 获取最终创建的文件名，会对下划线、自定义别名进行判断
func GetRealFileName(entityInfo *EntityInfo, nameType int) string {
	EntityName := entityInfo.EntityName
	if entityInfo.UserConfig != nil && entityInfo.UserConfig.AsName != "" {
		//转成小驼峰命名法
		EntityName = entityInfo.UserConfig.AsName
	}

	switch nameType {
	case NameCameBig: //实体名称，使用大驼峰命名法
		return NameToCameBig(EntityName)
	case NameCameSmall: //小驼峰命名法
		return NameToCameSmall(EntityName)
	case NameLine: //go文件名使用下划线命名法
		return NameToLine(EntityName)
	}

	return ""
}
